package com.zg.datapush.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.common.core.R;
import com.zg.common.core.dao.datapush.dao.DpInterfaceConfigDao;
import com.zg.common.core.dao.datapush.entity.DpInterfaceConfig;
import com.zg.datapush.vo.DpInterfaceConfigVo;
import com.zg.datapush.req.DpInterfaceConfigUpdateReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 接口配置服务
 * @author zg
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DpInterfaceConfigService {

    private final DpInterfaceConfigDao interfaceConfigDao;

    /**
     * 根据任务ID查询接口配置
     */
    public DpInterfaceConfigVo getByTaskId(Long taskId) {
        DpInterfaceConfig entity = interfaceConfigDao.getOne(
            new LambdaQueryWrapper<DpInterfaceConfig>()
                .eq(DpInterfaceConfig::getTaskId, taskId)
        );

        if (entity == null) {
            return null;
        }

        DpInterfaceConfigVo vo = new DpInterfaceConfigVo();
        BeanUtil.copyProperties(entity, vo);
        return vo;
    }

    /**
     * 新增接口配置
     */
    public R<DpInterfaceConfig> add(DpInterfaceConfigVo vo) {
        // 检查任务是否已有接口配置
        boolean exists = interfaceConfigDao.exists(
            new LambdaQueryWrapper<DpInterfaceConfig>()
                .eq(DpInterfaceConfig::getTaskId, vo.getTaskId())
        );
        if (exists) {
            return R.error("该任务已有接口配置");
        }

        DpInterfaceConfig entity = new DpInterfaceConfig();
        BeanUtil.copyProperties(vo, entity);

        interfaceConfigDao.save(entity);
        return R.success(entity);
    }

    /**
     * 更新接口配置
     */
    public R<DpInterfaceConfig> update(DpInterfaceConfigUpdateReq req) {
        DpInterfaceConfig entity = interfaceConfigDao.getById(req.getId());
        if (entity == null) {
            return R.error("接口配置不存在");
        }

        BeanUtil.copyProperties(req, entity);
        interfaceConfigDao.updateById(entity);
        return R.success(entity);
    }

    /**
     * 保存或更新接口配置
     */
    public R<DpInterfaceConfig> saveOrUpdate(DpInterfaceConfigUpdateReq req) {
        DpInterfaceConfig existing = interfaceConfigDao.getOne(
            new LambdaQueryWrapper<DpInterfaceConfig>()
                .eq(DpInterfaceConfig::getTaskId, req.getTaskId())
        );

        if (existing == null) {
            // 新增
            DpInterfaceConfig entity = new DpInterfaceConfig();
            BeanUtil.copyProperties(req, entity);
            interfaceConfigDao.save(entity);
            return R.success(entity);
        } else {
            // 更新
            BeanUtil.copyProperties(req, existing);
            interfaceConfigDao.updateById(existing);
            return R.success(existing);
        }
    }

    /**
     * 删除接口配置
     */
    public R<Void> deleteByTaskId(Long taskId) {
        interfaceConfigDao.remove(
            new LambdaQueryWrapper<DpInterfaceConfig>()
                .eq(DpInterfaceConfig::getTaskId, taskId)
        );
        return R.success();
    }
}
